/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.taobao.BuildFeedCatsJob
   Module Description   :

   Date Created      : 2011/3/24
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.taobao.batch;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.thread.batch.AbstractRunJob;
import com.yaodian100.ec.taobao.service.TaobaoService;

/**
 * @author jeffma
 * 
 */
public class BuildFeedCatsJob extends AbstractRunJob {

	private TaobaoService taobaoService;

	/** default constructor */
	public BuildFeedCatsJob(TaobaoService taobaoService) {
		super("BuildFeedCatsJob");
		this.taobaoService = taobaoService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.thread.batch.AbstractRunJob#execute()
	 */
	@Override
	public void execute() {
		String feedRootPath = ServerValue.getFrontApachePath() + "3party_ex" + File.separator + "taobao";

		// 建立 SellerCats.xml
		String feedCatPath = feedRootPath + File.separator + "SellerCats.xml";
		File feedCatFile = new File(feedCatPath);
		String feedCats = taobaoService.buildFeedCats(new Date(feedCatFile.lastModified()));
		if (feedCats != null) {
			try {
				if (feedCatFile.exists()) {
					String tmpFile = feedRootPath + File.separator + "bak" + File.separator + "SellerCats.xml."
							+ DateUtil.convertDateToString("yyMMddHHmm", new Date());
					FileUtils.moveFile(feedCatFile, new File(tmpFile));
				}
				FileUtils.writeStringToFile(feedCatFile, feedCats);
				logger.info("build " + feedCatPath);
			} catch (Exception e) {
				logger.error("build SellerCats.xml fail, exception:" + e.getMessage(), e);
			}
		} else {
			logger.info("build skip SellerCats.xml, feed modfiy:{}", new Date(feedCatFile.lastModified()));
		}
	}

}
